import service from "../../utils/request.js"

export interface User {
  username: string,
  password: string
}

const userLogin = (data: User) : Promise<any> => {
  console.log(data)
  return service.request({
    method: 'post',
    url: '/login',
    data: {
      username: data.username,
      password: data.password
    }
  })
}

// 退出登录，主要是将 用户的 redis 删除, 传值主要是防止如果使用store，将清除store放前面，这样这个方法就没用了
const Logout= (username: string) : Promise<any> => {
  return service.request({
    method: "post",
    url: "/logout",
    data: {
      username
    }
  })
}

const userRegister = (data: User) : Promise<any> => {
  return service.request({
    method: 'post',
    url: '/register',
    data: {
      username: data.username,
      password: data.password
    }
  })
}

export interface ChangePass {
  username: string,
  changeUsername?: string,
  oldPassword: string,
  newPassword: string
}

const changePassword = ({username, changeUsername, oldPassword, newPassword }: ChangePass ) : Promise<any> => {
  return service.request({
    method: 'post',
    url: "/changePassword",
    data: {
      username,
      changeUsername,
      oldPassword,
      newPassword
    }
  })
}

export { userLogin, Logout, userRegister, changePassword }